import { SideEnum } from "@@/enums/observation";
import { ISideStep } from "@@/typings/observation";
import { IEntityInfoPanel, IEntityLogPanel } from "@@/typings/panel";
import { LogEnum } from "@@/enums/observation";

export default class PanelUtilsTuiyan {
  /**
   * 获取实体列表
   * @param step
   * @returns
   */
  static entityList(step: ISideStep) {
    let tuiyanRedList: IEntityInfoPanel[] = [];
    let tuiyanBlueList: IEntityInfoPanel[] = [];
    let tuiyanRedMap = {};
    let tuiyanBlueMap = {};

    step.obj_list && step.obj_list.forEach((item) => {
      let tuiyanIconUrl = `/assets/scenario/images/${
        item.m_Side == 1 ? "red/" : "blue/"
      }${item.m_EntityType?.m_kind}-${item.m_EntityType?.m_domain}-${
        item.m_EntityType?.m_category
      }.png`;

      if (item.m_Side === SideEnum.red) {
        if (Object.prototype.hasOwnProperty.call(tuiyanRedMap, item.m_id)) {
          return;
        }
        tuiyanRedList.push({
          id: item.m_id,
          name: item.m_EntityName,
          iconUrl: tuiyanIconUrl,
          index: tuiyanRedList.length + 1
        });
        tuiyanRedMap;
      }
      if (item.m_Side === SideEnum.blue) {
        if (Object.prototype.hasOwnProperty.call(tuiyanBlueMap, item.m_id)) {
          return;
        }
        tuiyanBlueList.push({
          id: item.m_id,
          name: item.m_EntityName,
          iconUrl: tuiyanIconUrl,
          index: tuiyanBlueList.length + 1
        });
      }
    });

    return {
      red: tuiyanRedList,
      blue: tuiyanBlueList,
    };
  }

  /**
   * 获取日志
   * @param step
   * @returns
   */
  static entityLog(step: ISideStep, side: string, old: IEntityLogPanel[]) {
    let tuiyanLogs: IEntityLogPanel[] = [];
    let tuiyanOldDetectMap: { [propName: string]: string | number } = {};
    old.forEach((log) => {
      if (log.type === LogEnum.DETECT) {
        tuiyanOldDetectMap[log.id] = "";
      }
    });
    step.detect_list?.forEach((item) => {
      if (Object.prototype.hasOwnProperty.call(tuiyanOldDetectMap, item.m_id)) {
        return;
      }
      tuiyanLogs.push({
        id: item.m_id,
        type: LogEnum.DETECT,
        message:
          side === "red"
            ? "红方探测到" + item.m_EntityName
            : "蓝方探测到" + item.m_EntityName,
        time: new Date(),
      });
    });
    return tuiyanLogs;
  }

  /**
   * 获取所有实体ids
   */
  static entityIds(step: ISideStep) {
    let tuiyanIds:string[] = [];
    step.obj_list && step.obj_list?.forEach((item) => {
      tuiyanIds.push(item.m_id + "");
    })
    step.detect_list?.forEach((item) => {
      tuiyanIds.push(item.m_id + "");
    })
    return tuiyanIds;
  }
}
